const rollup = require('rollup');
const paths=require('../config/paths');
const path=require("path");
const fs=require("fs");
const inputConfig=require("./inputConfig")
const outputConfig=require("./outputConfig")

const buildFn=async (answers,files)=>{
    const {component,version}=answers
    const libPath=path.join(paths.libOutput,component);
    const esLibPath=path.join(libPath,"es");
    const cjsLibPath=path.join(libPath,"lib");
    const bundle = await rollup.rollup(inputConfig(component,files,version));
    const options=outputConfig(esLibPath,cjsLibPath);//提供两种模式 es6标准和cjs标准
    await bundle.write(options[0]);
    await bundle.write(options[1]);//因为rollUp不允许在路径外生成文件所以需要复制一下packageJson到项目目录
    await  fs.copyFileSync(path.join(esLibPath,"package.json"),path.join(libPath,"package.json"));
    await Promise.all([fs.unlinkSync(path.join(esLibPath,"package.json")),fs.unlinkSync(path.join(cjsLibPath,"package.json"))]);
    console.log("打包完成!");
}

module.exports=buildFn;